import os
import glob
import platform
import subprocess
import shutil


def files(curr_dir='.', ext='*.exe'):
    for i in glob.glob(os.path.join(curr_dir, ext)):
        yield i


def remove_files(rootdir, ext, show=False):
    for i in files(rootdir, ext):
        os.remove(i)


def list_idl():
    idls = []
    for i in files("src/idl", "*.idl"):
        idls.append(os.path.basename(i))
    return idls


def check_pb_env():
    error = os.system("protoc --version")
    if error != 0:
        print("Protbuf not found")
        return False
    else:
        out_bytes = subprocess.check_output(['protoc', '--version'])
        out_text = out_bytes.decode('utf-8')
        strs = out_text.split(" ")
        if int(strs[1].split('.')[0]) < 3:
            print("Need Protobuf version >= 3.0")
            return False
    return True


def check_git_env():
    error = os.system("git --version")
    if error != 0:
        print("git not found")
        return False
    return True


def check_golang_env():
    error = os.system("go version")
    if error != 0:
        print("golang not install")
        return False
    else:
        out_bytes = subprocess.check_output(['go', 'version'])
        out_text = out_bytes.decode('utf-8')
        strs = out_text.split(" ")
        if int(strs[2].split('.')[1]) < 13:
            print("Need golang version 1.13+")
            return False
    return True


def check_compiler_env():
    error = os.system("cmake --version")
    if error != 0:
        print("cmake not found")
        return False
    else:
        out_bytes = subprocess.check_output(['cmake', '--version'])
        out_text = out_bytes.decode('utf-8')
        strs = out_text.split(" ")
        if int(strs[2].split(".")[0]) < 3:
            print("Need cmake version >= 3")
            return False
        else:
            if int(strs[2].split(".")[1]) < 5:
                print("Need cmake version >= 3.5")
                return False
    if platform.system() == "Windows":
        out_bytes = subprocess.check_output(['cmake', '-h'])
        out_text = out_bytes.decode('utf-8')
        if out_text.find("Visual Studio 16 2019") == -1:
            print("Visual Studio 2019 not found")
            return False
        else:
            print("Visual Studio 16 2019")
    else:
        error = os.system("gcc --version")
        if error != 0:
            error = os.system("clang --version")
            if error != 0:
                print("gcc not found")
                print("clang not found")
                return False
        else:
            out_bytes = subprocess.check_output(['gcc', '--version'])
            out_text = out_bytes.decode('utf-8')
            strs = out_text.split(" ")
            if int(strs[2].split(".")[0]) < 7:
                print("Need gcc version >= 7")
                return False
    return True


def check_frontend(initializer):
    if not os.path.exists("tmp"):
        os.mkdir("tmp")
    os.chdir("tmp")

    if os.path.exists("rpc-frontend"):
        os.chdir("rpc-frontend")
        if os.system("git pull") != 0:
            return False
        os.chdir("..")
    else:
        if os.system("git clone https://gitee.com/dennis-kk/rpc-frontend") != 0:
            print("git clone https://gitee.com/dennis-kk/rpc-frontend failed")
            return False

    initializer.build_frontend()
    os.chdir("..")

    return True


def copy_dep_idl(basedir, idl_list, todir):
    if not basedir or basedir == "":
        basedir = "."

    if not todir or todir == "":
        todir = "."

    for idl in idl_list:
        dirifo = os.path.dirname(idl)
        if dirifo:
            pathcheck = todir
            for onedir in dirifo.split('/'):
                pathcheck += '/'+onedir
                if not os.path.exists(pathcheck):
                    os.mkdir(pathcheck)

        shutil.copyfile(basedir+'/'+idl, todir+'/'+idl)
    return True
